const fs = require('fs')

function isEmail(email) {
  const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  return reg.test(email)
}

function isDir(path) {
  return new Promise((res, rej) => {
    fs.stat(path, (err, stats) => {
      let _isDir = ''
      if (err) {
        rej(err)
      } else {
        _isDir = stats.isDirectory()
      }

      res(_isDir)
    })
  })
}

function isDirExists(path) {
  fs.stat(path, (err, stats) => {
    if (err) {
      fs.mkdir(path, { recursive: true }, (err, stats) => {
        if (err) {
          throw Error(err)
        } else {
          console.info(path + '  is create!')
          return path
        }
      })
    }
  })
}

function hasZh(string) {
  if (escape(string).indexOf( "%u" ) < 0) {
    return false
  } else {
    return true
  }
}

module.exports = {
  isEmail,
  isDir,
  isDirExists,
  hasZh
}
